msg_tool\scripts\kirikiri/
tjs2.rs1use crate::ext::io::*;
3use crate::scripts::base::*;
4use crate::types::*;
5use crate::utils::encoding::*;
6use crate::utils::struct_pack::*;
7use anyhow::Result;
8use serde::{Deserialize, Serialize};
9use std::io::{Read, Seek, Write};
10
11#[derive(Debug)]
12pub struct Tjs2Builder {}
14
15impl Tjs2Builder {
16 pub fn new() -> Self {
18 Self {}
19 }
20}
21
22impl ScriptBuilder for Tjs2Builder {
23 fn default_encoding(&self) -> Encoding {
24 Encoding::Utf16LE
25 }
26
27 fn build_script(
28 &self,
29 buf: Vec<u8>,
30 _filename: &str,
31 encoding: Encoding,
32 _archive_encoding: Encoding,
33 config: &ExtraConfig,
34 _archive: Option<&Box<dyn Script>>,
35 ) -> Result<Box<dyn Script>> {
36 Ok(Box::new(Tjs2::new(buf, encoding, config)?))
37 }
38
39 fn extensions(&self) -> &'static [&'static str] {
40 &["tjs"]
41 }
42
43 fn script_type(&self) -> &'static ScriptType {
44 &ScriptType::KirikiriTjs2
45 }
46
47 fn is_this_format(&self, _filename: &str, buf: &[u8], buf_len: usize) -> Option<u8> {
48 if buf_len >= 8 && buf.starts_with(b"TJS2100\0") {
50 return Some(40);
51 }
52 None
53 }
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57struct DataArea {
58 byte_array: Vec<u8>,
59 short_array: Vec<i16>,
60 long_array: Vec<i32>,
61 longlong_array: Vec<i64>,
62 double_array: Vec<f64>,
63 string_array: Vec<String>,
64 octet_array: Vec<Vec<u8>>,
65}
66
67impl StructUnpack for DataArea {
68 fn unpack<R: Read + Seek>(reader: &mut R, big: bool, encoding: Encoding) -> Result<Self> {
69 reader.align(4)?;
70 let start_loc = reader.stream_position()?;
71 let mut data_tag = [0; 4];
72 reader.read_exact(&mut data_tag)?;
73 if &data_tag != b"DATA" {
74 return Err(anyhow::anyhow!("Invalid DATA tag"));
75 }
76 let data_size = u32::unpack(reader, big, encoding)?;
77 let count = u32::unpack(reader, big, encoding)? as usize;
78 let byte_array = reader.read_exact_vec(count)?;
79 reader.align(4)?;
80 let short_count = u32::unpack(reader, big, encoding)? as usize;
81 let short_array = reader.read_struct_vec(short_count, big, encoding)?;
82 reader.align(4)?;
83 let long_count = u32::unpack(reader, big, encoding)? as usize;
84 let long_array = reader.read_struct_vec(long_count, big, encoding)?;
85 let longlong_count = u32::unpack(reader, big, encoding)? as usize;
86 let longlong_array = reader.read_struct_vec(longlong_count, big, encoding)?;
87 let double_count = u32::unpack(reader, big, encoding)? as usize;
88 let double_array = reader.read_struct_vec(double_count, big, encoding)?;
89 let str_count = u32::unpack(reader, big, encoding)? as usize;
90 let mut string_array = Vec::with_capacity(str_count);
91 for _ in 0..str_count {
92 let str_len = u32::unpack(reader, big, encoding)? as usize;
93 let str_bytes = reader.read_exact_vec(if encoding.is_utf16le() {
94 str_len * 2
95 } else {
96 str_len
97 })?;
98 let s = decode_to_string(encoding, &str_bytes, true)?;
99 reader.align(4)?;
100 string_array.push(s);
101 }
102 let octet_count = u32::unpack(reader, big, encoding)? as usize;
103 let mut octet_array = Vec::with_capacity(octet_count);
104 for _ in 0..octet_count {
105 let octet_len = u32::unpack(reader, big, encoding)? as usize;
106 let octet_bytes = reader.read_exact_vec(octet_len)?;
107 reader.align(4)?;
108 octet_array.push(octet_bytes);
109 }
110 let end_loc = reader.stream_position()?;
111 if end_loc - start_loc != data_size as u64 {
112 return Err(anyhow::anyhow!(
113 "DATA size mismatch: expected {}, got {}",
114 data_size,
115 end_loc - start_loc
116 ));
117 }
118 Ok(DataArea {
119 byte_array,
120 short_array,
121 long_array,
122 longlong_array,
123 double_array,
124 string_array,
125 octet_array,
126 })
127 }
128}
129
130impl StructPack for DataArea {
131 fn pack<W: Write>(&self, writer: &mut W, big: bool, encoding: Encoding) -> Result<()> {
132 writer.write_all(b"DATA")?;
133 let mut tmp = MemWriter::new();
134 tmp.write_struct(&(self.byte_array.len() as u32), big, encoding)?;
135 tmp.write_all(&self.byte_array)?;
136 tmp.align(4)?;
137 tmp.write_struct(&(self.short_array.len() as u32), big, encoding)?;
138 for v in &self.short_array {
139 tmp.write_struct(v, big, encoding)?;
140 }
141 tmp.align(4)?;
142 tmp.write_struct(&(self.long_array.len() as u32), big, encoding)?;
143 for v in &self.long_array {
144 tmp.write_struct(v, big, encoding)?;
145 }
146 tmp.write_struct(&(self.longlong_array.len() as u32), big, encoding)?;
147 for v in &self.longlong_array {
148 tmp.write_struct(v, big, encoding)?;
149 }
150 tmp.write_struct(&(self.double_array.len() as u32), big, encoding)?;
151 for v in &self.double_array {
152 tmp.write_struct(v, big, encoding)?;
153 }
154 tmp.write_struct(&(self.string_array.len() as u32), big, encoding)?;
155 for s in &self.string_array {
156 let encoded = encode_string(encoding, s, false)?;
157 let str_len = if encoding.is_utf16le() {
158 encoded.len() / 2
159 } else {
160 encoded.len()
161 };
162 tmp.write_struct(&(str_len as u32), big, encoding)?;
163 tmp.write_all(&encoded)?;
164 tmp.align(4)?;
165 }
166 tmp.write_struct(&(self.octet_array.len() as u32), big, encoding)?;
167 for o in &self.octet_array {
168 tmp.write_struct(&(o.len() as u32), big, encoding)?;
169 tmp.write_all(o)?;
170 tmp.align(4)?;
171 }
172 tmp.data.resize(tmp.pos, 0);
174 let data = tmp.into_inner();
175 writer.write_struct(&(data.len() as u32 + 8), big, encoding)?;
176 writer.write_all(&data)?;
177 Ok(())
178 }
179}
180
181#[derive(Debug)]
183pub struct Tjs2 {
184 data_area: DataArea,
185 remaing: Vec<u8>,
186 custom_yaml: bool,
187}
188
189impl Tjs2 {
190 pub fn new(buf: Vec<u8>, encoding: Encoding, config: &ExtraConfig) -> Result<Self> {
196 let mut reader = MemReader::new(buf);
197 let mut header = [0u8; 8];
198 reader.read_exact(&mut header)?;
199 if &header != b"TJS2100\0" {
200 return Err(anyhow::anyhow!("Invalid TJS2 header: {:?}", &header));
201 }
202 let _file_size = reader.read_u32()?;
203 let data_area = DataArea::unpack(&mut reader, false, encoding)?;
204 let mut remaing = Vec::new();
205 reader.read_to_end(&mut remaing)?;
206 Ok(Self {
207 data_area,
208 remaing,
209 custom_yaml: config.custom_yaml,
210 })
211 }
212}
213
214impl Script for Tjs2 {
215 fn default_output_script_type(&self) -> OutputScriptType {
216 OutputScriptType::Json
217 }
218
219 fn default_format_type(&self) -> FormatOptions {
220 FormatOptions::None
221 }
222
223 fn is_output_supported(&self, _: OutputScriptType) -> bool {
224 true
225 }
226
227 fn custom_output_extension<'a>(&'a self) -> &'a str {
228 if self.custom_yaml { "yaml" } else { "json" }
229 }
230
231 fn extract_messages(&self) -> Result<Vec<Message>> {
232 let mut messages = Vec::new();
233 for s in self.data_area.string_array.iter() {
234 messages.push(Message {
235 name: None,
236 message: s.clone(),
237 });
238 }
239 Ok(messages)
240 }
241
242 fn import_messages<'a>(
243 &'a self,
244 messages: Vec<Message>,
245 mut file: Box<dyn WriteSeek + 'a>,
246 _filename: &str,
247 encoding: Encoding,
248 replacement: Option<&'a ReplacementTable>,
249 ) -> Result<()> {
250 let mut data_area = self.data_area.clone();
251 data_area.string_array = messages
252 .iter()
253 .map(|m| {
254 let mut s = m.message.clone();
255 if let Some(table) = replacement {
256 for (from, to) in &table.map {
257 s = s.replace(from, to);
258 }
259 }
260 s
261 })
262 .collect();
263 file.write_all(b"TJS2100\0")?;
264 file.write_u32(0)?; data_area.pack(&mut file, false, encoding)?;
266 file.write_all(&self.remaing)?;
267 let file_size = file.stream_length()?;
268 file.write_u32_at(8, file_size as u32)?; Ok(())
270 }
271
272 fn custom_export(&self, filename: &std::path::Path, encoding: Encoding) -> Result<()> {
273 let s = if self.custom_yaml {
274 serde_yaml_ng::to_string(&self.data_area)?
275 } else {
276 serde_json::to_string_pretty(&self.data_area)?
277 };
278 let encoded = encode_string(encoding, &s, false)?;
279 let mut file = crate::utils::files::write_file(filename)?;
280 file.write_all(&encoded)?;
281 Ok(())
282 }
283
284 fn custom_import<'a>(
285 &'a self,
286 custom_filename: &'a str,
287 mut file: Box<dyn WriteSeek + 'a>,
288 encoding: Encoding,
289 output_encoding: Encoding,
290 ) -> Result<()> {
291 let data = crate::utils::files::read_file(custom_filename)?;
292 let s = decode_to_string(output_encoding, &data, true)?;
293 let data_area: DataArea = if self.custom_yaml {
294 serde_yaml_ng::from_str(&s)?
295 } else {
296 serde_json::from_str(&s)?
297 };
298 file.write_all(b"TJS2100\0")?;
299 file.write_u32(0)?; data_area.pack(&mut file, false, encoding)?;
301 file.write_all(&self.remaing)?;
302 let file_size = file.stream_length()?;
303 file.write_u32_at(8, file_size as u32)?; Ok(())
305 }
306}